package com.wsc.wisdomchinaqinghe.activitity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wsc.wisdomchinaqinghe.R;
import com.wsc.wisdomchinaqinghe.Tools.JsonTools;
import com.wsc.wisdomchinaqinghe.Tools.Tools;
import com.wsc.wisdomchinaqinghe.activitity.baseactivity.BaseFragmentActivity;
import com.wsc.wisdomchinaqinghe.application.MyApplication;
import com.wsc.wisdomchinaqinghe.callback.ContributorView;
import com.wsc.wisdomchinaqinghe.fragment.ReceptionCenterFragment;
import com.wsc.wisdomchinaqinghe.javaBean.ApplyTitleListBean;
import com.wsc.wisdomchinaqinghe.javaBean.RespObj;
import com.wsc.wisdomchinaqinghe.presenter.ContributorPresenter;
import com.wsc.wisdomchinaqinghe.view.PagerSlidingTabStrip;

import java.util.ArrayList;
import java.util.List;


/**
 * 受理中心
 */
public class ReceptionCenterActivity extends BaseFragmentActivity implements View.OnClickListener,ContributorView {
    private ImageView regist_back;
    private TextView tv_headtitle;
    private ViewPager viewPager;
    private PagerSlidingTabStrip tabs;// 新闻栏滑动条
    private Context context;
    private MyApplication myApplication;
    List<Fragment> lastlistfragmentView;
    private String name;
    private String cidNumber;
    private ContributorPresenter contributorPresenter;
    private final String FINDALLLISTFLAG="FINDALLLISTFLAG";
    private List<ApplyTitleListBean> listData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.peripheralsearch_layout);
        context=this;
        myApplication = (MyApplication)getApplication();
        listData=myApplication.getCenterApplyList();
        name=getIntent().getStringExtra("name");
        cidNumber=getIntent().getStringExtra("id");
        contributorPresenter = new ContributorPresenter(this, context);
        showData();
        initView();
    }

    private void initView()
    {
        regist_back=(ImageView) findViewById(R.id.regist_back);
        regist_back.setOnClickListener(this);
        tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
        tv_headtitle.setText(name);
        viewPager = (ViewPager)findViewById(R.id.viewPager);
        tabs = (PagerSlidingTabStrip)findViewById(R.id.tabs);
        tabs.setIndicatorHeight(Tools.dip2px(context, 3));
        tabs.setTextSize(Tools.dip2px(context, 17));
        tabs.setTextColor(getResources().getColor(R.color.text_color_one));
        tabs.setSelectedTextColor(getResources().getColor(R.color.little_white));

        if(listData!=null && listData.size()>0 )
        {
            showTabsViewPager();
        }
    }

    private void showData()
    {
        if(listData==null)
            contributorPresenter.getCommunityBussinessApplyTitleList(cidNumber,FINDALLLISTFLAG);
    }

    @Override
    public void onLoadContributorStart(String flag) {

    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            if(flag.equals(FINDALLLISTFLAG))
            {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code=respObj.getCode();
                if(code.equals("200")) {
                    String message = JsonTools.objectToJsonString(respObj.getMessage());
                    listData = JsonTools.jsonStringToList(message, ApplyTitleListBean.class);
                    if (listData != null && listData.size() > 0) {
                            myApplication.setCenterApplyList(listData);
                            showTabsViewPager();
                    }
                }

            }

        } catch (Exception e) {

        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error, String flag) {

    }



    private void  showTabsViewPager()
    {
        lastlistfragmentView = new ArrayList<>();
        List<String> lastEightModuleFragment = new ArrayList<>();
        List<String> nameList=new ArrayList<>();
        List<Boolean> isShowlist=new ArrayList<>();
        for(ApplyTitleListBean applyTitleListBean:listData)
        {
            Bundle bundle = new Bundle();
            bundle.putString("cid",cidNumber);
            bundle.putSerializable("object",applyTitleListBean);
            ReceptionCenterFragment receptionCenterFragment = ReceptionCenterFragment.newInstance(bundle);
            lastlistfragmentView.add(receptionCenterFragment);
            lastEightModuleFragment.add(ReceptionCenterFragment.class.getSimpleName() + applyTitleListBean.getId());
            nameList.add(applyTitleListBean.getTitle());
            if(applyTitleListBean.getType()==1||applyTitleListBean.getType()==10)
            {
                isShowlist.add(true);
            }else
            {
                isShowlist.add(false);
            }
        }
        myApplication.setInformationFragmentSelected(lastEightModuleFragment.get(0));
        viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(), lastlistfragmentView, nameList));// 设置viewpageradapter
        tabs.setIsShowPoint(isShowlist);
        tabs.setViewPager(viewPager, lastlistfragmentView, lastEightModuleFragment, this);
    }

    class ViewPagerAdapter extends FragmentStatePagerAdapter {

        private List<Fragment> listfragmentView;// viewList 集合
        private List<String> TITLES;

        public ViewPagerAdapter(FragmentManager fm, List<Fragment> listfragmentView, List<String> TITLES) {
            super(fm);
            this.listfragmentView = listfragmentView;
            this.TITLES = TITLES;
            // TODO Auto-generated constructor stub
        }

        @Override
        public CharSequence getPageTitle(int position) {
            // TODO Auto-generated method stub
            return TITLES.get(position);
        }

        @Override
        public Fragment getItem(int position) {
            // TODO Auto-generated method stub
            return listfragmentView.get(position);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return TITLES.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // TODO Auto-generated method stub

            Fragment ff = (Fragment) super.instantiateItem(container, position);
            ff.setTargetFragment(listfragmentView.get(position), position);

            return ff;
        }

        @Override
        public void destroyItem(View container, int position, Object object) {
            // TODO Auto-generated method stub
            super.destroyItem(container, position, object);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.regist_back:
                finish();
                break;
        }
    }

}
